<?php
namespace app\common\service;

use Obs\ObsClient;
class HuaWeiObs{

    const IMG_SIZE = 3145728;
    const VIDEO_SIZE = 31462680;
    const IMG_FORMAT = ['bmp', 'jpg', 'png', 'tif', 'gif', 'pcx', 'tga', 'exif', 'fpx', 'svg', 'psd', 'cdr', 'pcd', 'dxf', 'ufo', 'eps', 'ai', 'raw', 'WMF', 'webp', 'avif', 'apng'];
    const VIDDEO_FORMAT = ['avi', 'mpeg', 'mp4', 'wmv', 'flv', 'mov', 'mkv', 'rmvb', '3gp'];
    private static $key = 'JKVW6GZO1YLJLRNNWWPR';
    private static $secret = 'gny0QEZMiKjeHuC6UDw7wCBXiNC7NgCojkxk97Hs';
    private static $endpoint = 'obs.ap-southeast-1.myhuaweicloud.com';
    private static $backName = 'arab-php';
    private static $baseUrl = 'http://file.arab-alabo.com/'; // 展示图片的地址
    private static $downUrl = 'https://arab-php.obs.ap-southeast-1.myhuaweicloud.com:443/';

    private static function ObsObj(){
        return new ObsClient([
            'key' => self::$key,
            'secret' => self::$secret,
            'endpoint' =>self::$endpoint
        ]);
    }
    public static function Obs($fileName,$selfFilePath){

        $obsClient = self::ObsObj();
        $res = $obsClient->putObject([
                'Bucket' => self::$backName,
                'Key' => $fileName,
                'Body' => file_get_contents($selfFilePath)
        ]);
        if ($res ['HttpStatusCode'] == 200 && $res ['Reason'] == 'OK') {
            return ['code' => 1 ,'path'=>self::$baseUrl.$fileName, 'msg'=>'success'];
        } else {
            return ['code' => 2 ,'path'=>'', 'msg'=>'fail'];
        }
    }

    public static function uploadFile($arr = []){
        if(!empty($arr['dir']) && !empty($arr['tmp_name'])){
            $explode = explode(".", $arr['name']);
            $fileName = "mall_arab/".$arr['dir'].md5(implode("", $arr).mt_rand(1,9999999999999).time()).'.'.$explode[1];

            if(in_array($explode[1], self::IMG_FORMAT)){
                if($arr['size'] > self::IMG_SIZE){
                    return ['code' => 3 ,'path'=>'', 'msg'=>'图片太大，上传失败'];
                }
            }else if(in_array($explode[1], self::VIDDEO_FORMAT)){
                if($arr['size'] > self::VIDEO_SIZE){
                    return ['code' => 3 ,'path'=>'', 'msg'=>'视频太大，上传失败'];
                }
            }

            //开发-测试阶段---------------------------------------------------------------------
            // $_SERVER['SERVER_NAME'] = '1';
            $reTestUpload = self::TestHandle($arr['tmp_name'], $fileName);
            if($reTestUpload['status'] == true){
                return $reTestUpload;
            }
            //--------------------------------------------------------------------------------
            return self::Obs($fileName, $arr['tmp_name']);
        }
        return ['code' => 2 ,'path'=>''];
    }

    public static function TestHandle($tmp_name, $fileName){

        if($_SERVER['SERVER_NAME'] == 'arab.cn'){
            $fileDir = ROOT_PATH.'/public/uploads/'.date('Ymd');
            if(!is_dir($fileDir)){
                mkdir($fileDir, 0777, true);
            }
            $tmpFileName = '/'.array_reverse(explode('/', $fileName))[0];
            $url = 'http://'.$_SERVER['SERVER_NAME']."/uploads/".date('Ymd').'/'.$tmpFileName;

            if (move_uploaded_file($tmp_name, $fileDir.=$tmpFileName)) {
                return ['code' => 1 ,'path'=>$url,'status'=>true];
            } else {
                return ['code' => 2 ,'path'=>'','status'=>true];
            }
        }
        return ['status'=>false];
    }

    //删除
    public static function Obsdel($arr = [], $type = 0){

        $obsClient = self::ObsObj();
        $resp = null;
        $reVal = ['code'=>0, 'msg'=>''];
        if($type==0){
            if(!empty($arr['Key'])){
                $resp = $obsClient->deleteObject([
                    'Bucket' => self::$backName,
                    'Key' => $arr['Key'],
                    'VersionId'=>null
                ]);
            }
        }else if($type==1){
            if(!empty($arr[0]['Key'])){
                $resp = $obsClient->deleteObjects([
                    'Bucket'=>self::$backName,
                    'Objects'=>$arr,
                    'Quiet'=> false,
                ]);
            }
        }
        if($resp['HttpStatusCode'] == 200 && $resp['Reason'] == 'ok'){
            $reVal['code'] = 1;
        }else{
            $reVal['msg'] = $resp['Reason'];
        }
        return $reVal;
    }
}
